package com.ea.eadp.nexus.test.common;

import com.ea.eadp.nexus.test.common.CertInfo;
import com.ea.eadp.nexus.test.common.SystemProperties;
import com.ea.eadp.nexus.test.common.TestUtil;
import org.apache.geronimo.util.jce.X509Principal;
import org.apache.geronimo.util.jce.X509V1CertificateGenerator;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.BasicClientConnectionManager;

import java.io.FileOutputStream;
import java.math.BigInteger;
import java.security.*;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.io.InputStream;
import java.security.cert.X509Certificate;
import java.util.Date;
import java.util.Hashtable;
import java.util.Vector;

/**
 * Created by ChuanYe on 7/24/2014.
 */
public class HttpsClient  extends DefaultHttpClient {
    private final static String KEYPASS = "password";
    private final static int port = Integer.parseInt(SystemProperties.instance().getProperty(SystemProperties.HttpsClientPort));

    public HttpsClient(String keyStoreName) throws Exception {
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(getClass().getResourceAsStream("/config/" + keyStoreName), KEYPASS.toCharArray());

        TrustStrategy trustStrategy = new TrustStrategy() {
            @Override
            public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                return true;
            }
        };

        SSLSocketFactory ssf = new SSLSocketFactory("TLS", keyStore, KEYPASS, null, null,
                trustStrategy, new AllowAllHostnameVerifier());
        Scheme s = new Scheme("https", port, ssf);
        ClientConnectionManager ccm = getConnectionManager();
        ccm.getSchemeRegistry().register(s);
    }
}
